import {
	computed,
	toRefs
} from 'vue'
import Upgrade from '@/common/Upgrade.js'
import CryptoJS from 'crypto-js';

import {
	userInfo
} from '@/store/Pinia/userInfo.js'

import {
	time
} from '@/store/Pinia/time.js'
import {
	getLatestVersion
} from "@/api/systemInfo/index.js";
import akicryptojs from '@/uni_modules/aki-cryptojs/js_sdk/aki-cryptojs'
const AES_KEY = new Uint8Array([-31, -78, -99, 125, -119, -79, -19, 95, 112, 24, -104, -31, 21, 67, 86, 13]);
const key = CryptoJS.lib.WordArray.create(AES_KEY);

// AES编码
export function EncryptAES(word, mkey = key, miv) {
	let srcs = CryptoJS.enc.Utf8.parse(word); //传入的消息
	let viv = CryptoJS.enc.Utf8.parse(miv); //传入的秘钥偏移量
	let encrypted = CryptoJS.AES.encrypt(srcs, mkey, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.ciphertext.toString().toUpperCase();
}

// AES解码

export function DecryptAES(word, mkey = key, miv) {
	let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
	let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
	let viv = CryptoJS.enc.Utf8.parse(miv); //传入的秘钥偏移量
	let decrypt = CryptoJS.AES.decrypt(srcs, mkey, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
	return decryptedStr.toString();
}

export function CheckTenantId(tenantId) {

	if (tenantId === "" || tenantId == void 0 || tenantId === null) {
		if (authorizeCode != void 0) {
			try {
				let code = DecryptAES(authorizeCode.value)
				if (code != '') {
					checkTenantIdIsAllow('sx')

				} else {
					uni.showToast({
						title: '授权码错误',
						icon: 'none'
					})
				}
			} catch (e) {
				//TODO handle the exception
				console.log('cuow')
			}
		}

	} else {
		isShowPage.value = true
	}
}

export function Clear() {

	const keys = uni.getStorageInfoSync().keys;
	let keysToKeep = ['tenantId']

	keys.forEach((key) => {
		if (!keysToKeep.includes(key)) {
			uni.removeStorageSync(key);
		}
	});
}